﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RI
{
    public partial class formNews : Form
    {
        public formNews()
        {
            InitializeComponent();
            this.BindNews();
        }

        private void btnRelease_Click(object sender, EventArgs e)
        {
            this.BindNews();
        }

        /// <summary>
        /// 绑定新闻
        /// </summary>
        private void BindNews()
        {
            dgvNewList.DataSource = null;
            Business bus = new Business();
            dgvNewList.DataSource = bus.GetNews();
            txtTitle.Focus();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                Business bus = new Business();
                Business.News news = new Business.News();
                news.title = txtTitle.Text.Trim();
                news.html = rtxHtmlCode.Text.Trim();
                news.ubb = rtxUbbCode.Text.Trim();
                news.cat = cbxType.Text.Trim();
                bus.AddNews(news);

                BindNews();
                txtTitle.Text = "";
                rtxHtmlCode.Text = "";
                rtxUbbCode.Text = "";
                txtTitle.Focus();
            }
        }

        private bool CheckInput()
        {
            if (txtTitle.Text == string.Empty)
            {
                MessageBox.Show("标题不允许空", "提示");
                return false;
            }
            if (rtxHtmlCode.Text == string.Empty)
            {
                MessageBox.Show("Html代码内容不允许空", "提示");
                return false;
            }
            if (rtxUbbCode.Text == string.Empty)
            {
                MessageBox.Show("UBB代码内容不允许空", "提示");
                return false;
            }
            return true;
        }

        private void formMain_Load(object sender, EventArgs e)
        {
            cbxType.SelectedIndex = 0;
        }

        private void btnDelNew_Click(object sender, EventArgs e)
        {
            if (dgvNewList.SelectedRows.Count == 0)
            {
                MessageBox.Show("请选择要删除的行!", "提示",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                if (MessageBox.Show("确定要删除吗?", "提示",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    Business bus = new Business();
                    bus.DelNews(dgvNewList.SelectedRows[0].Cells[0].Value.ToString());
                    MessageBox.Show("删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    BindNews();
                }
            }
            
        }

        private void btnSeo_Click(object sender, EventArgs e)
        {
            //int contentLength = rtxContent.Text.Length; //内容总长度
            MessageBox.Show("暂时保留使用！", "提示",
                MessageBoxButtons.OK,
                MessageBoxIcon.Information);
        }
    }
}
